// Pine Script®
// © RakoQuant

//@version=6
indicator("DEMA Volatility SuperTrend | RakoQuant", shorttitle="DEMA VOL ST", overlay=true)

//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Inputs
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
groupSrc  = "Source"
groupBase = "DEMA Baseline"
groupVol  = "Volatility"
groupST   = "SuperTrend"
groupViz  = "Visuals"

src = input.source(ohlc4, "Source", group=groupSrc)

lenBase = input.int(35, "DEMA Length", minval=1, group=groupBase)

volMode = input.string("Raw Source", "Volatility Mode",
     options=["Residual vs Baseline", "Raw Source"], group=groupVol)

volLen = input.int(31, "Volatility Length (StDev)", minval=2, group=groupVol)
mult   = input.float(2.0, "Volatility Multiplier", minval=0.1, step=0.1, group=groupVol)

useClose = input.bool(true, "Flip Source = Close (else HL2)", group=groupST)

// Visual toggles
paintBars    = input.bool(true, "Paint Candles", group=groupViz)
showBase     = input.bool(true, "Show Baseline", group=groupViz)
showInactive = input.bool(true, "Show Inactive Bands (Faint)", group=groupViz)
showFill     = input.bool(true, "Fill Active Zone", group=groupViz)

//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
bullCol = color.rgb(0, 210, 255)
bearCol = color.rgb(170, 50, 255)

faintBull = color.new(bullCol, 80)
faintBear = color.new(bearCol, 80)
fillBull  = color.new(bullCol, 85)
fillBear  = color.new(bearCol, 85)

//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// DEMA
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
dema(series float x, int length) =>
    ema1 = ta.ema(x, length)
    ema2 = ta.ema(ema1, length)
    2.0 * ema1 - ema2

base = dema(src, lenBase)

//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Volatility bands
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
volSeries = volMode == "Residual vs Baseline" ? (src - base) : src
vol = ta.stdev(volSeries, volLen)

upperBasic = base + mult * vol
lowerBasic = base - mult * vol

//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// SuperTrend trailing logic on DEMA-vol bands
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
var float upperFinal = na
var float lowerFinal = na

upperFinal := na(upperFinal[1]) ? upperBasic :
     (upperBasic < upperFinal[1] or close[1] > upperFinal[1]) ? upperBasic : upperFinal[1]

lowerFinal := na(lowerFinal[1]) ? lowerBasic :
     (lowerBasic > lowerFinal[1] or close[1] < lowerFinal[1]) ? lowerBasic : lowerFinal[1]

flipSrc = useClose ? close : hl2

var int dir = 1
dir := na(dir[1]) ? 1 :
     flipSrc > upperFinal[1] ? 1 :
     flipSrc < lowerFinal[1] ? -1 :
     dir[1]

// Flip events
bullFlip = dir == 1 and dir[1] != 1
bearFlip = dir == -1 and dir[1] != -1

//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Plots
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
pBase = plot(showBase ? base : na, "DEMA Baseline", color=color.new(bullCol, 65), linewidth=1)

// Active bands
pBull = plot(dir == 1 ? lowerFinal : na, "Bull Band (Active)", color=bullCol, linewidth=2, style=plot.style_linebr)
pBear = plot(dir == -1 ? upperFinal : na, "Bear Band (Active)", color=bearCol, linewidth=2, style=plot.style_linebr)

// Inactive bands
plot(showInactive and dir != 1 ? lowerFinal : na, "Bull Band (Inactive)", color=faintBull, linewidth=1, style=plot.style_linebr)
plot(showInactive and dir != -1 ? upperFinal : na, "Bear Band (Inactive)", color=faintBear, linewidth=1, style=plot.style_linebr)

// Fill (between baseline and active band)
fill(pBase, pBull, color=showFill and dir == 1 ? fillBull : na)
fill(pBase, pBear, color=showFill and dir == -1 ? fillBear : na)

//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Candle painting (barcolor + plotcandle overlay)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
cCol = dir == 1 ? bullCol : bearCol
barcolor(paintBars ? cCol : na)

plotcandle(open, high, low, close, "Candles",
     color       = paintBars ? cCol : na,
     wickcolor   = paintBars ? cCol : na,
     bordercolor = paintBars ? cCol : na,
     force_overlay=true)

//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Alerts (candle-close synced)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
bullAlert = barstate.isconfirmed and bullFlip
bearAlert = barstate.isconfirmed and bearFlip

alertcondition(bullAlert, "Bull Flip", "DEMA Volatility SuperTrend turned BULL {{exchange}}:{{ticker}}")
alertcondition(bearAlert, "Bear Flip", "DEMA Volatility SuperTrend turned BEAR {{exchange}}:{{ticker}}")

